using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using T.FAS.Runtime.Base.Common;
using T.FAS.Runtime.Base.Dao;
using T.FAS.Runtime.Base.Entity;

namespace T.FAS.Runtime.Base.Service
{
    public class FASValidateRuleRTPClient
    {
        private static readonly FASValidateRuleRTPClient _instance = new FASValidateRuleRTPClient();
        private readonly ValidateRuleDao _dao = new ValidateRuleDao();
        public static FASValidateRuleRTPClient GetInstance()
        {
            return _instance;
        }

        private FASValidateRuleRTPClient()
        {
        }

        public List<ValidateRule> GetRules(Dictionary<string, object> extParams = null)
        {
            try
            {
                return FASValidateRuleCacheService.GetInstance().GetRules(extParams);
            }
            catch (Exception e)
            {
                throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_963668345", $"获取规则失败! msg:{e.Message}\r\ne:{e}")}");
            }
        }

        public ValidateRule GetRule(string ruleID)
        {
            try
            {
                return FASValidateRuleCacheService.GetInstance().GetRule(ruleID);
            }
            catch (Exception e)
            {
                throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_963668345", $"获取规则失败! msg:{e.Message}\r\ne:{e}")}");
            }
        }

        /// <summary>
        /// 更新规则的自定义属性
        /// </summary>
        /// <param name = "rule"></param>
        public void UpdateCustomRuleContent(ValidateRule rule)
        {
            if (rule == null || rule.ID == null)
                throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetText("T_FAS_fasr_Base_2103450637", $"更新自定义规则内容时传参有问题请检查")}");
            string userName = UserUtil.GetCurrentUserName();
            rule.LastModifyTime = DateTime.Now;
            rule.LastModifier = userName;
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    ValidateRule obj = _dao.GetObject(rule.ID);
                    obj.CustomRegContent = rule.CustomRegContent;
                    _dao.Update(new List<object>{obj});
                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new Exception($"{T.ServiceComponent.I18N.Service.TermService.Current.GetTextFormatted("T_FAS_fasr_Base_1286110121", "新建或者更新规则:{0}失败! msg:{1}\r\ne:{2}", rule?.Name, e.Message, e)}");
            }
        }

        /// <summary>
        /// 更新规则的自定义属性
        /// </summary>
        /// <param name = "rule"></param>
        public void UpdateCustomRuleContent(List<ValidateRule> rules)
        {
            foreach (var rule in rules)
            {
                UpdateCustomRuleContent(rule);
            }
        }
    }
}